package com.yonyou.cyxdms.sysAuth.service.controller.dealerchannel;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.controller.base.BaseController;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysAuth.service.dto.dealerchannel.DealerDTO;
import com.yonyou.cyxdms.sysAuth.service.dto.dealerchannel.DealerDetailDTO;
import com.yonyou.cyxdms.sysAuth.service.dto.dealerchannel.DealerInfoDTO;
import com.yonyou.cyxdms.sysAuth.service.dto.dealerchannel.DealerListDTO;
import com.yonyou.cyxdms.sysAuth.service.service.dealerchannel.DealerInfoService;
import com.yonyou.cyxdms.sysAuth.service.vo.companyorg.DealerOrgAreaVO;
import com.yonyou.cyxdms.sysAuth.service.vo.dealerchannel.SaleInfoVO;
import com.yonyou.cyxdms.sysmanage.client.domains.dto.DealerOrgInfoS;
import com.yonyou.cyxdms.sysmanage.client.vo.dealer.DealerInfoAVO;
import com.yonyou.cyxdms.sysmanage.client.vo.dealer.DealerInfoParentVO;
import com.yonyou.cyxdms.sysmanage.client.vo.dealer.DealerOrAscOrCompanyVO;
import com.yonyou.cyxdms.sysmanage.client.vo.dealer.DealerPictureVO;
import com.yonyou.cyxdms.sysmanage.client.vo.org.OrgParentVO;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 进销商信息
 *
 * @author zhangsx
 * @since 2018-10-22
 */
@RestController
@RequestMapping("/dealerInfo")
@Api(value = "经销商信息", tags = "经销商信息接口api")
public class DealerInfoController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(DealerInfoController.class);
    @Autowired
    public DealerInfoService dealerInfoService;

    /**
     * /分页查询数据
     *
     * @param queryCondition 查询条件
     * @author com。baomidou mybatisplas .core .metada .Ipage  分页对象
     * @author zhangsx
     * @since 2018/12/3
     */
    @ResponseBody
    @GetMapping("/")
    @SuppressWarnings("unchecked")
    public IPage<Map<String, Object>> getList(@RequestParam Map<String, Object> queryCondition, @RequestParam
            ("currentPage") int currentPage, @RequestParam("pageSize") int pageSize) {
        Page<Map<String, Object>> page = new Page(currentPage, pageSize);
        return dealerInfoService.selectPageBysql(page, queryCondition);
    }

    /**
     * 进行数据新增
     *
     * @param dealerInfoDTO
     * @return java.util.Map
     * @author zhangsx
     * @since 2019/1/9
     */
    @ResponseBody
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    @SuppressWarnings("unchecked")
    public Map<String, Object> insert(@RequestBody DealerInfoDTO dealerInfoDTO) {
        return dealerInfoService.insert(dealerInfoDTO);
    }

    /**
     * 进行数据修改
     *
     * @param id
     * @param dealerInfoDTO
     * @return java.util.Map
     * @author zhangsx
     * @since 2019/1/9
     */
    @ResponseBody
    @PutMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.CREATED)
    @SuppressWarnings("unchecked")
    public Map<String, Object> update(@PathVariable("id") Long id, @RequestBody DealerInfoDTO dealerInfoDTO) {
        return dealerInfoService.update(id, dealerInfoDTO);
    }

    /**
     * 处理来自dms的经销商信息
     *
     * @param dealerInfoDTO
     * @return java.lang.Long
     * @author zhangsx
     * @since 2018/10/22
     */
    @PostMapping(value = "/ccss/add", produces = {"application/json;charset=UTF-8"})
    public Long addDealerInfoFromDms(@RequestBody DealerInfoDTO dealerInfoDTO) {
        return dealerInfoService.insertDealerInfoFromDms(dealerInfoDTO);
    }

    /**
     * 更新经销商信息
     *
     * @param
     * @return void
     * @author zhangsx
     * @since 2018/10/31
     */
    @PostMapping(value = "/ccss/update", produces = {"application/json;charset=UTF-8"})
    public boolean updateDealerInfoFromDms(@RequestBody DealerInfoDTO dealerInfoDTO) {
        dealerInfoService.updateDealerInfoFromDms(dealerInfoDTO);
        return true;
    }

    /**
     * 通用根据经销商code取id
     *
     * @param dealerCode
     * @return java.lang.Long
     * @author zhangsx
     * @since 2018/11/12
     */
    @GetMapping(value = "/dealerId", produces = {"application/json;charset=UTF-8"})
    public Long getIdByCode(@RequestParam("dealerCode") String dealerCode) {
        return dealerInfoService.getDealerIdByDealerCode(dealerCode);
    }

    /**
     * 根据dealerId查询dealerCode
     *
     * @author dengqiang
     * @since 2019/1/4
     */
    @GetMapping(value = "/getDealerCode", produces = {"application/json;charset=UTF-8"})
    public String getDealerCode(@RequestParam("dealerId") Long dealerId) {
        return dealerInfoService.getDealerCodeByDealerId(dealerId);
    }

    /**
     * 根据dealerCode获取经销商通用接口
     *
     * @param dealerCode
     * @param appId
     * @return com.yonyou.cyx.dealer.service.entity.dto.dealerinfo.DealerInfoDTO
     * @author zhangsx
     * @since 2019/1/9
     */
    @GetMapping(value = "/getDealerInfo/byCode")
    @ApiOperation(value = "根据dealerCode获取经销商通用接口", notes = "暂用于小程序中获取顾问经销商名称")
    public DealerInfoDTO getDealerInfoByCode(@RequestParam(value = "dealerCode", required = true) String dealerCode,
                                             @RequestParam(value = "appId") String appId) {
        return dealerInfoService.getDealerInfoByCode(dealerCode, appId);
    }

    /***
     * 根据ID获取经销商信息
     *
     * @param id
     * @return java.util.Map
     * @author houkaihua
     * @since 2019/1/13
     */
    @ResponseBody
    @GetMapping(value = "/getDealerById")
    @SuppressWarnings("unchecked")
    public Map<String, Object> getDealerById(@RequestParam("id") Long id) {
        return dealerInfoService.getById(id);
    }

    /**
     * @param dealerListDTO
     * @return java.util.List<com.yonyou.cyx.dealer.service.entity.dto.dealerinfo.DealerInfoDTO>
     * @Description 获取经销商信息列表
     * @author liyuquan
     * @since 2019/1/23 14:38
     */
    @ResponseBody
    @PostMapping(value = "/dealerInfoList")
    @ApiOperation(value = "获取经销商信息列表", notes = "获取经销商信息列表")
    public List<DealerInfoDTO> dealerInfoList(@RequestBody DealerListDTO dealerListDTO) {
        logger.info("dealerInfoList:{}", dealerListDTO);
        //获取登陆信息
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        dealerListDTO.setAppId(loginInfoDto.getAppId());
        return dealerInfoService.dealerInfoList(dealerListDTO);
    }

    @ResponseBody
    @PostMapping(value = "/dealerDetail", produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "获取经销商详情", notes = "获取经销商详情")
    public DealerInfoDTO getDealerDetail(@RequestBody DealerDetailDTO detail) {
        logger.info("经销商详情dealerDetail:{}", detail);
        //获取登陆信息
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        detail.setAppId(loginInfoDto.getAppId());
        DealerInfoDTO dealerInfoDTO = dealerInfoService.dealerDetailInfo(detail);
        logger.info("数据库中查询的经销商信息：{}", dealerInfoDTO);
        return dealerInfoDTO;
    }


    /**
     * 根据经销商ID查询在售品牌ID列表
     *
     * @author dengqiang
     * @since 2018/12/13
     */
    @GetMapping(value = "/brandIdList", produces = {"application/json;" +
            "charset=UTF-8"})
    public List<Long> getSaleCarbrandIdList(@RequestParam("dealerId") Long dealerId) {
        //获取登陆信息
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        //获取appId
        return dealerInfoService.getSaleCarbrandIdList(dealerId, loginInfoDto.getAppId());
    }

    /**
     * //根据经销商id获取经销商试驾地址
     *
     * @author dengqiang
     * @since 2019/2/14
     */
    @GetMapping(value = "/queryDriveAddress", produces = {"application/json;" +
            "charset=UTF-8"})
    public String queryDriveAddress(@RequestParam("dealerId") Long dealerId) {
        //获取appId
        return dealerInfoService.getDriveAddress(dealerId);
    }


    /**
     * 查询经销商名称(根据appId和dearCode可以确定唯一性)
     *
     * @author dengqiang
     * @since 2019/5/16
     */
    @GetMapping(value = "/getDealerName", produces = {"application/json;" +
            "charset=UTF-8"})
    public String getDealerName(@RequestParam("appId") String appId, @RequestParam("dealerCode") String dealerCode) {
        //获取appId
        return dealerInfoService.getDealerName(appId, dealerCode);
    }

    /**
     * @description：获取修改的经销商
     * @author ：quankk
     * @date ：Created in 2019/1/22 11:40
     * @modified By：
     */
    @ApiOperation(value = "获取修改的经销商", notes = "获取修改的经销商")
    @PostMapping("/obtainDealer")
    public String obtainDealer() {
        return dealerInfoService.obtainDealer();
    }


    /**
     * 无条件查询经销商信息
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/8/16
     */
    @GetMapping("/getDealer")
    @ApiOperation(value = "获得经销商信息", notes = "获得经销商信息")
    public List<DealerDTO> getDealer(@RequestParam("currentPage") Long currentPage,
                                     @RequestParam("size") Long size) {
        return dealerInfoService.getDealer(currentPage, size);
    }

    /**
     * 获取登录用户公司信息
     *
     * @auther: 李贺
     * @since: 2019/9/6
     */
    @GetMapping("/getDealerOrAscOrCompany")
    @ApiOperation(value = "获取登录用户公司信息", notes = "获取登录用户公司信息")
    @ApiImplicitParams({@ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public DealerOrAscOrCompanyVO getDealer(@RequestParam(value = "ownerCode", required = false) String ownerCode) {
        return dealerInfoService.getDealerOrAscOrCompany(ownerCode);
    }


    /**
     * 获取登录用户公司信息通过code或者名称
     * @auther: luoyong
     * @since: 2022/4/6
     */
    @GetMapping("/getCompanyByCodeOrName")
    @ApiOperation(value = "获取登录用户公司信息", notes = "获取登录用户公司信息")
    @ApiImplicitParams({@ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public DealerOrAscOrCompanyVO getCompanyByCode(@RequestParam(value = "dealerCode", required = false) String ownerCode) {
        return dealerInfoService.getCompanyByCodeOrName(ownerCode);
    }


    /**
     * @param ownerCodes 要查找的所有code
     * @param isBlack    是否包含以上code true :not in ,false:in
     * @param appId
     * @param ownerCode  模糊查询
     * @return:
     * @auther: tanjie
     * @since: 2019/9/23
     */
    @GetMapping("/getDealerInfoByCodes")
    @ApiOperation("/根据codes获取经销商信息")
    public List<Map<String, String>> getDealerInfoByCodes(@RequestParam(required = false) List<String> ownerCodes,
                                                          @RequestParam(value = "isBlack", required = false) Boolean isBlack,
                                                          @RequestParam(value = "appId", required = true) String appId,
                                                          @RequestParam(value = "ownerCode", required = false) String ownerCode,
                                                          @RequestParam(value = "orgId", required = false) String orgId,
                                                          @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                                          @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                                          @RequestParam(value = "companyId", required = false) Long companyId) {
        return dealerInfoService.getDealerInfoByCodes(ownerCodes, isBlack, appId, ownerCode, orgId, currentPage, pageSize, companyId);
    }

    /**
     * 获取经销商上级code
     *
     * @param ownerCode
     * @return:
     * @auther: tanjie
     * @since: 2019/9/24
     */
    @GetMapping("/getDealerHeightLevelCode")
    @ApiOperation("获取经销商的组织公司等code信息")
    public Map<String, String> getDealerHeightLevelCode(@ApiParam("经销商Code") @RequestParam("ownerCode") String ownerCode,
                                                        @RequestParam("appId") String appId) {

        return this.dealerInfoService.getDealerHeightLevelCode(ownerCode, appId);
    }

    /**
     * 获取所有销售公司信息
     *
     * @auther: 李贺
     * @since: 2019/10/9
     */
    @GetMapping("/getSaleInfo")
    @ApiOperation("获取所有销售公司信息")
    @ApiImplicitParams({@ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public IPage<SaleInfoVO> getSaleInfo(@RequestParam("currentPage") int currentPage, @RequestParam("pageSize") int pageSize) {
        Page<SaleInfoVO> page = new Page(currentPage, pageSize);
        return dealerInfoService.getSaleInfo(page);
    }

    /**
     * 新增修改销售公司
     *
     * @auther: 李贺
     * @since: 2019/10/9
     */
    @PutMapping("/saleInfo")
    @ApiOperation("新增修改销售公司")
    @ApiImplicitParams({@ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public void addSaleInfo(@RequestBody SaleInfoVO saleInfoVO) {
        dealerInfoService.addSaleInfo(saleInfoVO);
    }

    /**
     * 根据ORGID或者经销商名称获取经销商信息
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/10/24
     */
    @GetMapping("getDealerByorgId")
    @ApiOperation("根据ORGID或者经销商名称获取经销商信息")
    public List<DealerInfoDTO> getDealerByorgId(@RequestParam(value = "search", required = false) String orgId,
                                                @RequestParam(value = "ownerName", required = false) String ownerName) {
        return this.dealerInfoService.getDealerByorgId(orgId, ownerName);
    }

    /**
     * 获取经销商公司的 经销商类型
     *
     * @param:
     * @return:
     * @auther: 李贺
     * @since: 2019/11/4
     */
    @GetMapping("/getDealerType")
    @ApiOperation("获取经销商公司的 经销商类型")
    public Integer getDealerType() {
        return this.dealerInfoService.getDealerType();
    }


    /**
     * 是否有售后
     *
     * @param:
     * @return:
     * @auther: 李贺
     * @since: 2019/11/4
     */
    @GetMapping("/afterSale")
    @ApiOperation("是否有售后 10041001：有，10041002：无")
    public Integer afterSale() {
        return this.dealerInfoService.afterSale();
    }

    /**
     * 根据条件查询经销商信息
     *
     * @auther: 李贺
     * @since: 2019/11/13
     */
    @GetMapping("/getDealerInfos")
    @ApiOperation("根据条件查询经销商信息")
    @ApiImplicitParams({@ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public List<DealerInfoParentVO> getDealerInfos(@RequestParam(value = "orgId", required = false) Long orgId, @RequestParam(value = "provinceId", required = false) Long provinceId) {
        return dealerInfoService.getDealerInfos(orgId, provinceId);
    }

    /**
     * 根据省市区查询经销商信息
     *
     * @auther: 李贺
     * @since: 2019/11/15
     */
    @GetMapping("/getDealerInfosByCity")
    @ApiOperation("根据省市区查询经销商信息")
    @ApiImplicitParams({@ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public List<DealerInfoParentVO> getDealerByCity(@RequestParam(value = "provinceId", required = false) Integer provinceId,
                                                    @RequestParam(value = "cityId", required = false) Integer cityId,
                                                    @RequestParam(value = "countyId", required = false) Integer countyId) {
        return dealerInfoService.getDealerByCity(provinceId, cityId, countyId);
    }

    /**
     * 远端调用获取经销商orgid
     *
     * @param orgId
     * @param appId
     * @param dealerCode
     * @param companyId
     * @param dealerName
     * @return
     * @auther: tanjie
     */
    @GetMapping("/getDealerBysaleArea")
    public List<DealerOrgAreaVO> getDealerBySaleArea(@RequestParam(value = "orgId", required = false) String orgId,
                                                     @RequestParam("appId") String appId,
                                                     @RequestParam(value = "dealerCode", required = false) String dealerCode,
                                                     @RequestParam("companyId") Long companyId,
                                                     @RequestParam(value = "dealerName", required = false) String dealerName) {
        LoginInfoDto login = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        login.setAppId(appId);
        login.setCompanyId(companyId);

        return this.dealerInfoService.getDealerBySaleArea(orgId, dealerCode, dealerName);
    }

    /**
     * 根据dealerCODES批量获取ORGID
     *
     * @return
     * @auther: tanjie
     */
    @PostMapping("/getDealerBysaleAreaBatch")
    List<DealerOrgAreaVO> getDealerBysaleAreaBatch(@RequestBody DealerOrgInfoS dealerOrgInfoS) {
        return this.dealerInfoService.getDealerBysaleAreaBatch(dealerOrgInfoS.getOrgId(), dealerOrgInfoS.getAppId(), dealerOrgInfoS.getDealerCodes(), dealerOrgInfoS.getCompanyId(), dealerOrgInfoS.getDealerName());
    }

    /**
     * 根据ownerCode返回经销商
     *
     * @param ownerCode
     * @return
     * @auther: maxignjain
     */
    @GetMapping("/getDealerByOwner")
    public DealerInfoAVO getDealerByOwner(@RequestParam(value = "ownerCode", required = true) String ownerCode) {
        return dealerInfoService.getDealerByOwner(ownerCode);
    }

    @GetMapping("/getAllDealerCode")
    public List<String> getAllDealerCode(@RequestParam("appId") String appId) {
        List<DealerDTO> allDealerCode = this.dealerInfoService.getAllDealerCode(appId);
        return allDealerCode != null ? allDealerCode.stream().map(DealerDTO::getDealerCode).collect(Collectors.toList()) : null;
    }

    /**
     * 获得经销商所属区域
     * 李贺
     *
     * @param
     * @return
     */
    @GetMapping("/getSaleArea")
    public List<OrgParentVO> getSaleArea(@RequestParam("dealerIds") List<Long> dealerIds) {
        return dealerInfoService.getSaleArea(dealerIds);
    }

    /**
     * 保存经销商营业时间和门店图片
     * <p>
     * 李贺
     */
    @ApiOperation("保存经销商营业时间和门店图片")
    @PostMapping("/dealerPicture")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public void setDealerPicture(@RequestBody DealerPictureVO dealerPictureVO) {
        dealerInfoService.setDealerPicture(dealerPictureVO);
    }

    /**
     * 获取经销商营业时间和门店图片
     * <p>
     * 李贺
     */
    @ApiOperation("获取经销商营业时间和门店图片")
    @GetMapping("/dealerPictures")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public List<DealerPictureVO> getDealerPictures(@RequestParam(value = "dealerIds", required = false) String dealerIds,
                                                   @RequestParam(value = "dealerCodes", required = false) String dealerCodes) {
        if (StringUtils.isNullOrEmpty(dealerCodes) && StringUtils.isNullOrEmpty(dealerIds)) {
            return null;
        }
        return dealerInfoService.getDealerPictures(dealerIds, dealerCodes);
    }

    /**
     * 获取所有销售部门
     * <p>
     * 李贺
     */
    @ApiOperation("获取所有销售部门Id")
    @GetMapping("/dealer/orgs")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public List<Long> listDealerOrgs() {
        return dealerInfoService.listDealerOrgs();
    }

}